﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ComponentModel;

namespace HospitalSystem.Core
{
    public class servicerecordLog
    {




        public void BindData(GridView GridView1,String svccode)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT ACTCode,ACTDesc FROM Activity where SVCCode=@svccode1", scon);
            cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();
        }




        public void procesbutton(Label SelectStat, GridView GridView1, Label psnumber, GridView GridView11,String svccode)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            SelectStat.Text = string.Empty;
            bool selectorChecked3 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("ACTCode");


            dt2.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    selectorChecked3 = true;
                    TextBox rb = row.FindControl("TextBox3") as TextBox;
                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string ACTCode = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    SelectStat.Text += string.Format("{0} has been added <br />", ACTCode);
                    //  string CID = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string note = rb.Text;
                    dt2.Rows.Add(ACTCode, note);

                }
            }
            SelectStat.Visible = selectorChecked3;

            readtodatatable(psnumber,GridView1,GridView11,svccode);
            deledata(GridView11,psnumber,GridView1,svccode);



            for (int i = 0; i < dt2.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [ServiceProvisionDetails] (ACTCode,PSNumber,Note) VALUES('" + dt2.Rows[i]["ACTCode"].ToString() + "',@PSNumber,'" + dt2.Rows[i]["note"].ToString() + "')", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", psnumber.Text);
                command.ExecuteNonQuery();
                scon.Close();

            }

            readtodatatable(psnumber, GridView1, GridView11, svccode);
        }



        public void readtodatatable(Label psnumber, GridView GridView1, GridView GridView11, String svccode)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dt = new DataTable();
            string svccode1 = svccode;
            SqlCommand cmd = new SqlCommand("SELECT a.[ACTCode] as ACTCode,b.Note as Note  FROM [Activity] a  join [ServiceProvisionDetails] b on a.ACTCode=b.ACTCode  where a.SVCCode=@svccode1 and b.PSNumber=@PSNumber", scon);
            cmd.Parameters.AddWithValue("@PSNumber", psnumber.Text);
            cmd.Parameters.AddWithValue("@svccode1", svccode);

            SqlDataReader reader8 = cmd.ExecuteReader();

            dt.Columns.Add("ACTCode");
            dt.Columns.Add("Note");
            while (reader8.Read() != false)
            {
                dt.Rows.Add(reader8["ACTCode"].ToString(), (reader8["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader8.Close();


            for (int j = 0; j < dt.Rows.Count; j++)
            {
                string a = dt.Rows[j]["ACTCode"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CaseSelector3");
                    TextBox tb = (TextBox)GR.FindControl("TextBox3");
                    if (a == b)
                    {

                        tb.Text = dt.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }
            GridView11.DataSource = dt;
            GridView11.DataBind();

            scon.Close();


        }



        public void deledata(GridView GridView11, Label psnumber, GridView GridView1,String svccode)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            DataTable dt3 = new DataTable();
            dt3.Columns.Add("ACTCode");


            foreach (GridViewRow row in GridView11.Rows)
            {

                DataRow dr3;
                dr3 = dt3.NewRow();
                string ACTCode = GridView11.Rows[row.RowIndex].Cells[0].Text;

                dt3.Rows.Add(ACTCode);

            }




            for (int i = 0; i < dt3.Rows.Count; i++)
            {


                SqlCommand command = new SqlCommand("delete from [ServiceProvisionDetails] where  PSNumber=@PSNumber and ACTCode='" + dt3.Rows[i]["ACTCode"].ToString() + "'", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", psnumber.Text);
                command.ExecuteNonQuery();
                scon.Close();


            }

            BindData(GridView1,svccode);

        }

    }


}